package com.ruoyi.config

import com.alipay.easysdk.factory.Factory
import com.alipay.easysdk.kernel.Config
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.ruoyi.back.domain.AliPaySetting
import com.ruoyi.back.service.AliPaySettingService
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.stereotype.Component

@Component
@EnableConfigurationProperties(CpAliPayProperties::class, PayNotifyParam::class)
class CpPayUtils(val config: CpAliPayProperties, val payNotifyParam: PayNotifyParam, aliPaySettingService: AliPaySettingService) {


    init {
        aliPaySettingService.getOne(
            KtQueryWrapper(AliPaySetting::class.java)
                .eq(AliPaySetting::isUse, true)
                .last("limit 1")
        )?.let {
            Factory.setOptions(Config().apply {
                protocol = config.protocol
                gatewayHost = config.gatewayHost
                signType = config.signType
                appId = it.appId
                merchantPrivateKey = it.privateKey
                alipayPublicKey = it.publicKey
                notifyUrl = "http://face.hfyanlv.com/stage-api/api/pay/aliNotify"
            })
        }
    }
}